package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.bean.SystemCodes;
import org.opentides.editor.BaseEntityEditor;
import org.opentides.service.SystemCodesService;
import org.opentides.util.StringUtil;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.entity.PatientMedicalProgram;
import com.eleventwelve.iris.entity.Staff;
import com.eleventwelve.iris.service.PatientMedicalProgramService;
import com.eleventwelve.iris.service.PatientService;
import com.eleventwelve.iris.service.StaffService;

/**
 * 
 * @author Ashley
 *
 */
@SuppressWarnings("deprecation")
public class PatientProgramFormController extends SimpleFormController{
	private PatientService patientService;
	private SystemCodesService systemCodesService;
	private StaffService staffService;
	private PatientMedicalProgramService patientMedicalProgramService;
	
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		PatientMedicalProgram patientMedicalProgram = new PatientMedicalProgram();
		String action = request.getParameter("action");
		
		if(action.equalsIgnoreCase("update")){
			String id = request.getParameter("id");
			if(!StringUtil.isEmpty(id) && StringUtils.isNumeric(id)){
				patientMedicalProgram = patientMedicalProgramService.load(id);
			}
		} else {
			Patient patient = loadPatient(request.getParameter("patientId"));
			if(patient!=null){
				patientMedicalProgram.setPatient(patient);
			}
			
			patientMedicalProgram.setResultStatus(systemCodesService.findByKey("NEW"));
		}
		
		
		return patientMedicalProgram;
	}
	
	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("staffs", staffService.findAll());
		map.put("resultStatuses", systemCodesService.findSystemCodesByCategory("RESULT_STATUS"));
		
		return map;
		
	}
	
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Patient.class, new BaseEntityEditor(patientService));
		binder.registerCustomEditor(SystemCodes.class, new BaseEntityEditor(systemCodesService));
		binder.registerCustomEditor(Staff.class, new BaseEntityEditor(staffService));
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		PatientMedicalProgram patientMedicalProgram = (PatientMedicalProgram) command;
		patientMedicalProgramService.save(patientMedicalProgram);
		
		String action = request.getParameter("action");
		
		return new ModelAndView("redirect:/patient-program.jspx?action="+action+"&id="+patientMedicalProgram.getId());
		
	}
	
	private Patient loadPatient(String patientId){
		Patient patient = null;
		if(!StringUtil.isEmpty(patientId)){
			patient = patientService.load(patientId);
		}
		return patient;
	}

	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public SystemCodesService getSystemCodesService() {
		return systemCodesService;
	}

	public void setSystemCodesService(SystemCodesService systemCodesService) {
		this.systemCodesService = systemCodesService;
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

	public PatientMedicalProgramService getPatientMedicalProgramService() {
		return patientMedicalProgramService;
	}

	public void setPatientMedicalProgramService(
			PatientMedicalProgramService patientMedicalProgramService) {
		this.patientMedicalProgramService = patientMedicalProgramService;
	}
}
